package com.hospital.wx.controller;

import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.SelectMoreDescriptionByIdForm;
import com.hospital.wx.service.HealthGuidesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/health/guides")
@Tag(name = "HealthGuidesController", description = "健康咨询信息查询")
public class HealthGuidesController {
    @Autowired
    private HealthGuidesService healthGuidesService;

    @GetMapping("/selectByViews")
    @Operation(summary = "根据浏览数查询两条最火咨询")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectByViews() {
        try {
            ArrayList<HashMap> healthGuides = healthGuidesService.selectByViews();
            return CommonResult.ok().put("healthGuides", healthGuides);
        } catch (Exception e) {
            // 记录异常日志
            log.error("查询过程中出现异常", e);
            // 返回通用错误响应，给前端提示
            return CommonResult.failed("查询过程最火两条出错");
        }
    }

    @GetMapping("/selectAll")
    @Operation(summary = "查询所有健康咨询")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectAll() {
        try {
            ArrayList<HashMap> healthGuides = healthGuidesService.selectAll();
            return CommonResult.ok().put("AllHealthGuide", healthGuides);
        } catch (Exception e) {
            // 记录异常日志
            log.error("查询过程中出现异常", e);
            // 返回通用错误响应，给前端提示
            return CommonResult.failed("查询过程出错");
        }
    }

    @PostMapping("/selectMoreDescriptionById")
    @Operation(summary = "根据id查询相关咨询详细信息")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectMoreDescriptionById(@Valid @RequestBody SelectMoreDescriptionByIdForm form) {
        try {
            HashMap healGuides = healthGuidesService.selectMoreDescriptionById(form.getId());
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, healGuides);
        } catch (Exception e) {
            log.error("查询过程中出现错误", e);
            return CommonResult.failed("查询过程中出现错误");
        }
    }
}
